package io.gitee.chearnee.fw.openapi.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import io.gitee.chearnee.fw.openapi.config.OpenApiProperties;
import io.gitee.chearnee.fw.openapi.constant.enums.HeadersEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/9/23 21:54
 */
@Component
@AllArgsConstructor
@Slf4j
public class TokenUtil {

  private final OpenApiProperties openApiProperties;

  public static void main(String[] args) {
    String appid = "fw3bjlzfsho5nrxvxz";
    String appkey = "sfalgczp9zarzvz9o06lgfy1kdjanlu6ypuop90e1lq7qvr8a3jkwfnxeikebbzl";
//    HashMap<String, String> stringStringHashMap = new HashMap<>();
//    stringStringHashMap.put("projectid", "1");
//    stringStringHashMap.put("username", "五冶公司");
//    stringStringHashMap.put("grantType", "client_credential");
//    sign(appid, appkey,
//        stringStringHashMap);
    long xx = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    String s = RandomUtil.randomString(8);
    HashMap<String, String> map = new HashMap<>();
//    map.put("projectid", "1");
//    map.put("username", "五冶公司");
//    map.put("grantType","client_credential");
    map.put("entId", "");
    map.put("nextId", "22168");
    map.put("projectId", "78");
//    map.put("userType","EMPLOYEE");
    map.put("userType", "WORKER");
//    map.put("startTime", "2021-01-01 00:00:00");
//    map.put("endTime", "2021-10-01 00:00:00");
    sign(appid, appkey, map);
//    sign1(appid
//        ,appkey
//        ,xx
//        ,s,map);

  }

  private static void sign(String appid, String appKey, Map<String, String> bodyMap) {
    if (bodyMap == null) {
      bodyMap = new HashMap<>();
    }
    TreeMap<String, String> stringStringTreeMap = new TreeMap<>(bodyMap);
    StringBuilder stringBuilder = new StringBuilder();
    stringStringTreeMap
        .forEach((key, value) -> stringBuilder.append(key).append("=").append(value).append("&"));
    stringStringTreeMap.clear();
    stringStringTreeMap.put(HeadersEnum.APPID.getValue(), appid);
    stringStringTreeMap.put(HeadersEnum.TIMESTAMP.getValue(),
        LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + "");
    stringStringTreeMap.put(HeadersEnum.NONCE_STRING.getValue(), RandomUtil.randomString(8));
    stringBuilder.append(HeadersEnum.APPID.getValue()).append("=")
        .append(appid).append("&");
    stringBuilder.append(HeadersEnum.TIMESTAMP.getValue()).append("=")
        .append(stringStringTreeMap.get(HeadersEnum.TIMESTAMP.getValue())).append("&");
    stringBuilder.append(HeadersEnum.NONCE_STRING.getValue()).append("=")
        .append(stringStringTreeMap.get(HeadersEnum.NONCE_STRING.getValue())).append("&");
    stringBuilder.append(HeadersEnum.APP_KEY.getValue()).append("=")
        .append(appKey);
    String signature = SecureUtil.md5(stringBuilder.toString()).toUpperCase();
    stringStringTreeMap.put(HeadersEnum.SIGNATURE.getValue(), signature);
    stringStringTreeMap.forEach((key, value) -> {
      System.out.println(key + ":" + value);
    });
  }
  /**
   * 生成token
   *
   * @param appId
   * @param appKey
   * @param salt
   * @return
   */
  public String produceToken(String appId, String appKey, String salt) {
    StringBuilder stringBuilder = new StringBuilder(appId);
    stringBuilder.append(salt);
    stringBuilder.append(appKey);
    stringBuilder.append(RandomUtil.randomString(8));
    stringBuilder.append(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
    return SecureUtil.md5(stringBuilder.toString());
  }

  /**
   * 生成token
   *
   * @param appId
   * @param appKey
   * @return
   */
  public String produceToken(String appId, String appKey) {
    return produceToken(appId, appKey, openApiProperties.getSalt());
  }
}
